package com.lagou.wwyan.rpc.sirealizer;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class RpcDecoder extends ByteToMessageDecoder {
    private Class<?> clazz;

    private Serializer serializer;

    public RpcDecoder(Class<?> clazz, Serializer serializer) {

        this.clazz = clazz;

        this.serializer = serializer;

    }
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List out) throws Exception {

//        ByteBuf bytes = byteBuf.getBytes(0, dst);

        int anInt = byteBuf.readInt();
        byte[] dst = new byte[anInt];
        byteBuf.readBytes(dst);
        Object deserialize = serializer.deserialize(clazz, dst);
        out.add(deserialize);
    }
}
